<template>
  <div class="container" ref="mainCon">
    <el-row
      class="toolbar searchInputWidth"
      style="padding-bottom: 0; padding-top: 24px !important"
    >
      <el-form :inline="true" :model="filterField" ref="searchConditions">
        <el-col :lg="8" :xl="6">
          <el-form-item label="姓名" class="row-padding-bottom">
            <el-input
              v-model="filterField.name"
              placeholder="请输入姓名"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :lg="8" :xl="6">
          <el-form-item label="有效标志" class="row-padding-bottom">
            <el-select
              v-model="filterField.status"
              filterable
              placeholder="请选择有效标志"
              clearable
              @change="bankSelectChange"
              @clear="bankSiteClear"
            >
              <el-option
                v-for="item in jobStatusOptions"
                :key="item.code"
                :label="item.name"
                :value="item.code"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :lg="8" :xl="6">
          <el-form-item label="">
            <el-button
              type="primary"
              icon="icon iconfont iconchazhao1"
              @click="searchList"
              class="searchBtn"
            >
              <span>搜索</span>
            </el-button>
          </el-form-item>
          <!-- <el-form-item>
                        <div class="uploadBox">
                            <el-button class="commonBut saveBtn funComBut" @click="goFile">
                                <span class="opetareText">导入</span>
                            </el-button>
                            <div class="upLoadDom" style="display: none">
                                <form id="unemployImportBox">
                                    <input
                                        type="file"
                                        ref="unemployfiles"
                                        id="unemployExcel"
                                        name="file"
                                        @change="unemployImportfileChange"
                                    />
                                </form>
                            </div>
                        </div>
                    </el-form-item> -->
        </el-col>
      </el-form>
    </el-row>
    <div class="spacing"></div>
    <div
      ref="tableBox"
      :class="hiddleToolBar ? 'tableBoxCur tableBox' : 'tableBoxCur'"
    >
      <div style="margin-bottom: 24px">
        <div class="uploadBox">
          <el-button
            class="commonBut saveBtn funComBut"
            icon="icon iconfont icondaoru"
            @click="goFile"
          >
            <span class="opetareText">导入</span>
          </el-button>
          <div class="upLoadDom" style="display: none">
            <form id="unemployImportBox">
              <input
                type="file"
                ref="unemployfiles"
                id="unemployExcel"
                name="file"
                @change="unemployImportfileChange"
              />
            </form>
          </div>
        </div>
      </div>
      <el-table
        ref="tableBox1"
        :max-height="tableHeight"
        :data="tableList"
        v-loading="loading"
        :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
        highlight-current-row
        stripe
        @sort-change="sortChange"
        style="border: 1px solid #dfe6ec"
        border
      >
        <el-table-column prop="name" label="姓名"></el-table-column>
        <el-table-column
          prop="sex"
          label="性别"
          :formatter="formatTd"
        ></el-table-column>
        <el-table-column
          prop="idcard"
          label="身份证号"
          :formatter="formatTd"
          min-width="150"
        >
          <template slot-scope="scope">
            <p>{{ scope.row.idcard | deseHandler(4, 4, '*') || '--' }}</p>
          </template>
        </el-table-column>
        <el-table-column
          prop="tel"
          label="联系电话"
          align="center"
          :formatter="formatTd"
          min-width="150"
        >
          <template slot-scope="scope">
            <p>{{ scope.row.tel | deseHandler(3, 4, '*') || '--' }}</p>
          </template>
        </el-table-column>
        <el-table-column
          label="有效标志"
          align="center"
          :formatter="formatTd"
          min-width="150"
        >
          <template slot-scope="scope">
            <p>{{ scope.row.status || '--' }}</p>
          </template>
        </el-table-column>
        <el-table-column
          prop="birthday"
          label="出生日期"
          sortable="custom"
          align="center"
          :formatter="formatTd"
          min-width="180"
        ></el-table-column>
        <el-table-column
          label="文化程度"
          align="center"
          width="200"
          prop="edu"
        ></el-table-column>
        <el-table-column label="失业原因" width="230" prop="loseReason">
          <template slot-scope="scope">
            <copyText
              :tableIndex="scope.$index"
              :copyTextName="scope.row.loseReason"
              :columnName="'loseReason'"
            ></copyText>
          </template>
        </el-table-column>
        <el-table-column
          label="失业时间"
          align="center"
          sortable="custom"
          min-width="180"
          prop="loseTime"
        ></el-table-column>
        <el-table-column
          label="备注"
          align="center"
          min-width="180"
          prop="remark"
        ></el-table-column>
        <el-table-column label="操作" align="left" fixed="right" width="110">
          <template slot-scope="scope">
            <el-link type="primary" @click="toDetail(scope.$index, scope.row)"
              >查看详情</el-link
            >
            <!-- <span v-else>--</span> -->
          </template>
        </el-table-column>
        <!-- 表格数据为空时，页面显示 -->
        <div slot="empty" v-show="isShow">
          <emptyTable emptyText="暂无相关信息"></emptyTable>
        </div>
      </el-table>
      <Pagination
        ref="pagination"
        @getList="getTableList"
        :filterField="filterField"
        :requestUrl="requestUrl"
      ></Pagination>
    </div>
  </div>
</template>

<script>
import common from '../../kits/common'
export default {
  name: 'unemploymentInfo',
  data() {
    return {
      advanced: false,
      //表格数据
      tableList: [],
      requestUrl: 'api-r/employeejob/expand/list',
      areaId: '',
      bankOptions: [],
      //搜索条件
      filterField: {
        type: 'out',
        name: '',
        status: '',
      },
      isShow: false, //列表加载
      loading: true,
      isShowDetail: '',
      bankId: '',
      bankSiteOptions: [],
      api: '',
      tableHeight: null,
      hiddleToolBar: false,
      jobStatusOptions: [],
    }
  },
  created() {
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData()
    })
    this.getFindbytype('JOB_STATUS')

    // this.filterField.serverArea = this.$store.state.areaInfoArr.areaId;
    // this.isShowDetail = this.$store.state.buttonLimitsArr.indexOf("bankSalary:detail") > -1 || this.$store.state.buttonLimitsArr.indexOf("mCsSalaryList:detail") > -1 || this.$store.state.buttonLimitsArr.indexOf("bankSalaryPayList:detail") > -1 || this.$store.state.buttonLimitsArr.indexOf("TBankSalaryPayList:detail") > -1;
  },
  activated() {
    window.addEventListener('scroll', this.handleScroll, true)
    this.$nextTick(function () {
      // this.$refs.pagination.getTableListData();
      this.$refs.tableBox1.doLayout()
    })
  },
  deactivated() {
    let that = this
    window.removeEventListener('scroll', that.handleScroll, true)
  },
  beforeDestroy() {
    let that = this
    window.removeEventListener('scroll', that.handleScroll, true)
  },
  mounted() {
    window.addEventListener('scroll', this.handleScroll, true)
    let that = this
    this.$nextTick(function () {
      let tableHeightCur = this.$refs.tableBox1.$el.clientHeight
      let tableTop = this.$refs.tableBox.getBoundingClientRect().top
      if (tableHeightCur + tableTop > window.innerHeight) {
        this.tableHeight =
          this.$refs.mainCon.getBoundingClientRect().height - 86
      } else {
        document.getElementsByClassName('tableBoxCur').length &&
          (document.getElementsByClassName('tableBoxCur')[0].style.height =
            window.innerHeight - tableTop - 28 + 'px')
      }

      window.onresize = function () {
        if (!that.$refs.tableBox1) {
          return
        }
        let tableHeightCur = that.$refs.tableBox1.$el.clientHeight
        let tableTop = that.$refs.tableBox.getBoundingClientRect().top
        if (tableHeightCur + tableTop > window.innerHeight) {
          that.tableHeight =
            that.$refs.mainCon.getBoundingClientRect().height - 86
        } else {
          document.getElementsByClassName('tableBoxCur').length &&
            (document.getElementsByClassName('tableBoxCur')[0].style.height =
              window.innerHeight - tableTop - 40 + 'px')
        }
      }
    })
  },
  methods: {
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    getTableList(data) {
      this.tableList = data
      this.loading = false
      if (this.tableList.length < 1) {
        this.isShow = true
      }
    },
    // 搜索
    searchList() {
      this.$refs.pagination.getTableListData()
    },
    //跳转详情
    toDetail(index, row) {
      this.$router.push({
        path: '/home/eZhoubigScreen/infoDetail',
        name: 'infoDetail',
        query: {
          id: row.id,
          type: 'unEmploy',
          name: row.name,
          status: row.status,
        },
      })
      this.$store.commit('CHANGE_SLIDER_NAME', {
        oldUrl: '/home/eZhoubigScreen/employmentInfoTab',
        newUrl: this.$router.currentRoute.fullPath,
      })
    },
    getFindbytype(type) {
      this.axios({
        method: 'post',
        url: 'api-bs/base/dicitem/findbytype',
        data: { type: type },
      })
        .then((response) => {
          if (!response.data.hasError) {
            this.jobStatusOptions = response.data.result
          }
        })
        .catch((error) => {})
    },
    handleScroll(e) {
      this.$nextTick(function () {
        if (
          this.$refs.tableBox &&
          this.$refs.tableBox.getBoundingClientRect().top < 128
        ) {
          // this.tableHeight = this.$refs.mainCon.getBoundingClientRect().height - 84
          this.tableHeight = window.innerHeight - 230

          if (document.getElementsByClassName('tableBoxCur').length) {
            document.getElementsByClassName('tableBoxCur')[0].style.height =
              null
          }
          this.hiddleToolBar = true
        } else {
          this.hiddleToolBar = false
        }
      })
    },
    goFile() {
      this.$refs.unemployfiles.click()
    },
    unemployImportfileChange(e) {
      var that = this
      var Filedata = $('#unemployExcel').val()
      if (!/\.(xls|xlsx)$/.test(Filedata)) {
        this.$message({
          message: 'excel类型必须是.xls,xlsx中的一种',
          type: 'warning',
        })
        return
      }

      setTimeout(function () {
        that.uploadFileByAjaxSubmit(
          'api-r/employeejob/expand/importoutjob',
          that.success,
          that.error
        )
      }, 500)
    },
    uploadFileByAjaxSubmit(url, successCallBack, errorCallBack) {
      const vm = this
      var token = localStorage.getItem('access_token')
      var options = {
        url: common.uploadUrl + url,
        type: 'POST',
        // data: vm.flieUpload,
        headers: { Authorization: 'Bearer ' + token },
        contentType: false,
        processData: false,
        success: function (data) {
          $('#unemployExcel').val('')
          successCallBack(data)
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
          $('#unemployExcel').val('')
          errorCallBack(XMLHttpRequest, textStatus, errorThrown)
        },
      }

      //三种方式均可
      $('#unemployImportBox').ajaxSubmit(options)
      return false
    },
    success(data) {
      if (!data.hasError) {
        this.$message({
          message: data.msg,
          type: 'success',
        })
        this.$nextTick(function () {
          this.$refs.pagination.getTableListData()
        })
      } else {
        this.$message({
          message: data.errorMsg,
          type: 'error',
        })
      }
    },
    //根据状态码判断是否是401
    error(XMLHttpRequest, textStatus, errorThrown) {
      if (XMLHttpRequest.status === '401' || XMLHttpRequest.status === '403') {
        localStorage.setItem('access_token', '')
        localStorage.setItem('refresh_token', '')
        localStorage.setItem('userName', '')
        // 只有在当前路由不是登录页面才跳转
        if (router.currentRoute.path !== '/login/loginFirst') {
          router.replace({
            path: '/login/loginFirst',
          })
        }
        //清除tab栏数据
        this.$store.state.sliderBar = []
        localStorage.removeItem('sliderBar')
      } else {
        this.$message({
          message: '数据请求失败',
          type: 'warning',
        })
      }
    },
  },
}
</script>

<style scoped lang="less">
.container {
  // padding:24px;
  .uploadBox {
    position: relative;
    .uploadImg {
      width: 96px;
      height: 36px;
      padding: 0px;
      color: #009588;
      border: 1px solid #009588;
      cursor: pointer;
    }
    .upLoadDom {
      position: absolute;
      width: 96px;
      height: 36px;
      top: 0;
      left: 0;
      opacity: 0;
      line-height: 0px;
      form {
        input {
          width: 96px;
          height: 36px;
          line-height: 0px;
          cursor: pointer;
        }
      }
    }
  }
}
.topTitle {
  height: 67px;
  background-color: #e4ebf1;
  padding-left: 24px;
  // font: bold 18px/67px Microsoft YaHei;
}
.mainCon {
  top: 198px;
}
@media only screen and (max-width: 1366px) {
  /deep/.searchInputWidth .el-form--inline .el-form-item__label {
    width: 80px;
  }
}
@import '../../../static/css/filterArea.less';
</style>
